# coding=utf-8
from . import admin
from flask import render_template, request, jsonify, make_response
from dbConnection.MysqlConn import Mysql
from app import util, PAGE_NUM, page_code
from app.util import login_required
import sys
import time, math

reload(sys)
sys.setdefaultencoding('utf8')


# 新闻列表
@admin.route('/admin/news_list.html')
@login_required
def news_list():
    c_type = get_all_type()
    return render_template('admin/content/news_list.html', c_type=c_type)


# 获取新闻列表数据
@admin.route('/admin/news_list_post', methods=['post'])
@login_required
def news_list_post():
    parms = ['share_type', 'news_type']
    maps = {}
    for d in parms:
        maps[d] = request.values.get(d)
    p = request.values.get('p')
    c_num = (int(p) - 1) * PAGE_NUM
    sql = "select a.*, t.type_name FROM news_list a LEFT JOIN news_type t ON t.tid = a.news_type WHERE 1=1"
    sql2 = "select count(*) con FROM news_list WHERE 1=1"
    if maps['share_type']:
        sql += " and a.share_type = '%s'" % maps['share_type']
        sql2 += " and share_type = '%s'" % maps['share_type']
    if maps['news_type']:
        sql += " and a.news_type = '%s'" % maps['news_type']
        sql2 += " and news_type = '%s'" % maps['news_type']
    sql += " order by a.publish_time desc limit %d, %d" % (c_num, PAGE_NUM)
    mysql = Mysql()
    res = mysql.getAll(sql, None)
    cont = mysql.getOne(sql2, None)
    mysql.dispose()
    pages = int(math.ceil(cont['con'] / float(PAGE_NUM))) if cont else 0
    route_path = 'get_data'
    html_str = page_code.page_code_html_post(int(p), route_path, pages) if pages > 0 else ''
    if res:
        for d in res:
            d['publish_time'] = str(d['publish_time'])
        data = {
            'status': 1,
            'res': res,
            'page': html_str,
            'p': p
        }
    else:
        data = {
            'status': 2,
            'page': html_str
        }
    return jsonify(data)


# 删除新闻
@admin.route('/admin/delete_news_post', methods=['post'])
@login_required
def delete_news_post():
    uid = request.values.get('uid')
    sql = "delete from news_list WHERE id = '%s'" % uid
    mysql = Mysql()
    res = mysql.delete(sql, None)
    mysql.dispose()
    data = {}
    data['status'] = 1
    if res > 0:
        data['status'] = 1
    return jsonify(data)


# 类型列表
@admin.route('/admin/type_list.html')
@login_required
def type_list():
    sql = "select * FROM news_type"
    mysql = Mysql()
    res = mysql.getAll(sql, None)
    mysql.dispose()
    return render_template('admin/content/type_list.html', res=res)


# 修改类型状态
@admin.route('/admin/type_update_status', methods=['post'])
@login_required
def type_update_status():
    tid = request.values.get('tid')
    status = request.values.get('status')
    sql = "update news_type SET status = '%s' WHERE tid = '%s'" % (status, tid)
    mysql = Mysql()
    res = mysql.update(sql, None)
    mysql.dispose()
    data = {}
    data['status'] = 0
    if res > 0:
        data['status'] = 1
    return jsonify(data)


# 获取所有类型
def get_all_type():
    sql = "select * FROM news_type"
    mysql = Mysql()
    res = mysql.getAll(sql, None)
    mysql.dispose()
    return res